﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Common;
using Enter.Common.statics;
using Enter.Common.Helper;
using System.Web;
using Enter.Services.Statistics;
using Enter.DataProxy.Statistics;
using Enter.Common.Statistics;

namespace Enter.Services.IcoStatistics
{
	public partial class Statistics : IStatistics
	{
		ImgManager imgmanager = new ImgManager();

		public byte[] ImgSrc(Enter.Services.Statistics.DTO.UserPvInfo info)
		{
			throw new NotImplementedException();
		}

		public int GetUserId(HttpContextBase HttpContext)
		{
			int userid = 0;
			string url = HttpContext.Request.Url.AbsoluteUri.ToLower();
			int len = url.IndexOf("/img/");
			int gif = url.IndexOf(".gif");
			if (len > 0 && gif>0)
			{
				url = url.Substring(len+5, url.Length - 4- len-5);
				string code = SecurityHelper.DecryptDES(url, Enter.Common.Statistics.Statistics.Url_Key);
				string[] res = code.Split('|');
				if (res.Length == 3)
				{
					string code2 = RandomCode.RemoveSeats(res[1]);
					userid = code2.To<int>(0);
				}
			}
			return userid;
		}

		public string GenerateImgSrc(int uid)
		{
			string code = string.Format(Enter.Common.Statistics.Statistics.UrlUser, 
				RandomCode.GenerateCode(3), RandomCode.FillSeats(uid.ToString(), 8),RandomCode.GenerateCode(2));

			string result = SecurityHelper.EncryptDES(code, Enter.Common.Statistics.Statistics.Url_Key);
			return result;
		}

		public string ShowSrc(ImgStateType ImgState)
		{
			if (ImgState != ImgStateType.列表 && ImgState != ImgStateType.删除)
				return imgmanager.Show_DefImg((int)ImgState);
			return "";
		}
	}
}
